home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / boot / czesc_2 / toolmanager / source / library / libinit.c < prev    next >
C/C++ Source or Header  |  1993-05-15  |  9KB  |  294 lines

  1. /*
  2.  * LibInit.c  V2.1
  3.  *
  4.  * shared library C stub
  5.  *
  6.  * (c) 1990-1993 Stefan Becker
  7.  */
  8.  
  9. #include "ToolManagerLib.h"
  10.  
  11. /* library name & id string */
  12. #define INTTOSTR(a) #a
  13. const char LibName[]=TMLIBNAME;
  14. const char LibId[]="$VER: " TMLIBNAME " " INTTOSTR(TMLIBVERSION) "."
  15.                    INTTOSTR(TMLIBREVISION) " (" __COMMODORE_DATE__ ")\r\n";
  16.  
  17. /* prototypes for library management functions */
  18. __geta4 static struct Library *LibOpen(__A6 struct Library *, __D0 ULONG);
  19. __geta4 static BPTR            LibClose(__A6 struct Library *);
  20. __geta4 static BPTR            LibExpunge(__A6 struct Library *);
  21.         static ULONG           LibReserved(void);
  22. __geta4 static void            QuitToolManager(void);
  23. __geta4 struct TMHandle       *AllocTMHandle(void);
  24. __geta4 static void            FreeTMHandle(__A0 struct TMHandle *);
  25. __geta4 static BOOL            CreateTMObjectTagList(__A0 struct TMHandle *,
  26.                                                      __A1 char *, __D0 ULONG,
  27.                                                      __A2 struct TagItem *);
  28. __geta4 static BOOL            DeleteTMObject(__A0 struct TMHandle *,
  29.                                               __A1 char *);
  30. __geta4 static BOOL            ChangeTMObjectTagList(__A0 struct TMHandle *,
  31.                                                      __A1 char *,
  32.                                                      __A2 struct TagItem *);
  33.  
  34. /* library functions table */
  35. static const APTR LibVectors[]={
  36.                                 /* Standard functions */
  37.                                 (APTR) LibOpen,
  38.                                 (APTR) LibClose,
  39.                                 (APTR) LibExpunge,
  40.                                 (APTR) NULL,
  41.  
  42.                                 /* Library specific functions */
  43.                                 (APTR) LibReserved, /* reserved for ARexx */
  44.                                 (APTR) QuitToolManager,
  45.                                 (APTR) AllocTMHandle,
  46.                                 (APTR) FreeTMHandle,
  47.                                 (APTR) CreateTMObjectTagList,
  48.                                 (APTR) DeleteTMObject,
  49.                                 (APTR) ChangeTMObjectTagList,
  50.  
  51.                                 /* Table end */
  52.                                 (APTR) -1
  53.                                 };
  54.  
  55. /* misc. data */
  56. static BPTR LibSegment=NULL;
  57. static struct Library *PrivateDOSBase=NULL;
  58. static struct Task *HandlerTask;
  59. struct Library *SysBase=NULL;
  60. struct Library *LibBase=NULL;
  61. BOOL Closing=FALSE;
  62. const char DosName[]="dos.library";
  63.  
  64. /* Prototype & pragma for local calls to CreateNewProc() */
  65. struct Task *MyCreateNewProc(struct TagItem *tags);
  66. #pragma libcall PrivateDOSBase MyCreateNewProc 1f2 101
  67.  
  68. /* library init routine */
  69. __geta4 struct Library *LibInit(__A0 BPTR LibSegList)
  70. {
  71.  struct Library *MyLib;
  72.  
  73.  /* Get ExecBase */
  74.  SysBase=*(struct Library **) 4; /* AbsExecBase */
  75.  
  76.  /* Open dos.library */
  77.  if (!(PrivateDOSBase=OpenLibrary(DosName,37))) return(0);
  78.  
  79.  LibSegment=LibSegList;
  80.  
  81.  if (!(LibBase=MyLib=MakeLibrary(LibVectors, NULL, NULL,
  82.                                  sizeof(struct Library), NULL))) {
  83.   CloseLibrary(PrivateDOSBase);
  84.   return(0);
  85.  }
  86.  
  87.  MyLib->lib_Node.ln_Type=NT_LIBRARY;
  88.  MyLib->lib_Node.ln_Name=LibName;
  89.  MyLib->lib_Flags=LIBF_CHANGED|LIBF_SUMUSED;
  90.  MyLib->lib_Version=TMLIBVERSION;
  91.  MyLib->lib_Revision=TMLIBREVISION;
  92.  MyLib->lib_IdString=(APTR) LibId;
  93.  AddLibrary(MyLib);
  94.  
  95.  DEBUG_PRINTF("Init Lib: %08lx ",MyLib);
  96.  DEBUG_PRINTF("Seg: %08lx\n",LibSegment);
  97.  
  98.  return(MyLib);
  99. }
  100.  
  101. /* shared library open function */
  102. __geta4 static struct Library *LibOpen(__A6 struct Library *lib,
  103.                                        __D0 ULONG version)
  104. {
  105.  /* Handle special case: OpenCnt=0 & Handler is just closing down */
  106.  if ((lib->lib_OpenCnt == 0) && Closing) return(NULL);
  107.  
  108.  /* Handler active? Try to start it... */
  109.  if (!LibraryPort && !(HandlerTask=MyCreateNewProc(HandlerProcessTags)))
  110.   return(NULL);
  111.  
  112.  /* Oh another user :-) */
  113.  lib->lib_OpenCnt++;
  114.  
  115.  /* Reset delayed expunge flag */
  116.  lib->lib_Flags&=~LIBF_DELEXP;
  117.  
  118.  /* Return library pointer */
  119.  DEBUG_PRINTF("Open Lib: %ld\n",lib->lib_OpenCnt);
  120.  return(lib);
  121. }
  122.  
  123. /* shared library close function */
  124. __geta4 static BPTR LibClose(__A6 struct Library *lib)
  125. {
  126.  /* Open count already zero or more than one user? */
  127.  if ((lib->lib_OpenCnt == 0) || (--lib->lib_OpenCnt > 0)) return(NULL);
  128.  
  129.  /* Is handler active? Yes, send him a signal if he should shut down */
  130.  if (LibraryPort && Closing) Signal(HandlerTask,SIGBREAKF_CTRL_F);
  131.  
  132.  /* Is the delayed expunge bit set?  Yes, try to remove the library */
  133.  if (lib->lib_Flags & LIBF_DELEXP) return(LibExpunge(lib));
  134.  
  135.  /* No. Don't remove library now */
  136.  return(NULL);
  137. }
  138.  
  139. /* shared library expunge function */
  140. __geta4 static BPTR LibExpunge(__A6 struct Library *lib)
  141. {
  142.  DEBUG_PRINTF("Expunge Lib: %08lx ",lib);
  143.  DEBUG_PRINTF("Seg: %08lx\n",LibSegment);
  144.  
  145.  /* Does no-one use library now or is handler active/closing down?? */
  146.  if ((lib->lib_OpenCnt > 0) || LibraryPort || Closing) {
  147.   /* No, library still in use -> set delayed expunge flag */
  148.   lib->lib_Flags|=LIBF_DELEXP;
  149.   return(NULL);
  150.  }
  151.  
  152.  /* Yes, remove library and free resources */
  153.  Remove(&lib->lib_Node);
  154.  FreeMem((void *) ((ULONG) lib-lib->lib_NegSize),
  155.          lib->lib_NegSize+lib->lib_PosSize);
  156.  if (PrivateDOSBase) {
  157.   CloseLibrary(PrivateDOSBase);
  158.   PrivateDOSBase=NULL;
  159.  }
  160.  
  161.  /* return BPTR to our seglist */
  162.  DEBUG_PUTSTR("Removing library...\n");
  163.  return(LibSegment);
  164. }
  165.  
  166. /* Reserved function, returns NULL */
  167. static ULONG LibReserved(void)
  168. {
  169.  return(NULL);
  170. }
  171.  
  172. /* Set quit flag for handler process */
  173. __geta4 static void QuitToolManager(void)
  174. {
  175.  /* Set flag */
  176.  if (LibraryPort && !Closing) Closing=TRUE;
  177. }
  178.  
  179. /* Send IPC message */
  180. static BOOL SendIPC(struct TMHandle *handle)
  181. {
  182.  /* Handler ready? */
  183.  if (LibraryPort) {
  184.   /* Yep, send message */
  185.   PutMsg(LibraryPort,(struct Message *) handle);
  186.  
  187.   /* Wait on reply */
  188.   WaitPort(handle->tmh_Msg.mn_ReplyPort);
  189.  
  190.   /* Get reply */
  191.   GetMsg(handle->tmh_Msg.mn_ReplyPort);
  192.  
  193.   /* get return code */
  194.   return(handle->tmh_Command);
  195.  }
  196.  
  197.  /* Oops nobody listening :-( */
  198.  return(FALSE);
  199. }
  200.  
  201. /* Allocate a TMHandle */
  202. __geta4 void *AllocTMHandle(void)
  203. {
  204.  struct TMHandle *handle;
  205.  
  206.  DEBUG_PRINTF("AllocTMHandle() called.\n");
  207.  
  208.  /* Allocate memory for handle structure */
  209.  if (handle=AllocMem(sizeof(struct TMHandle),MEMF_PUBLIC)) {
  210.   struct MsgPort *rp;
  211.  
  212.   /* Create IPC Port */
  213.   if (rp=CreateMsgPort()) {
  214.    /* Init message */
  215.    handle->tmh_Msg.mn_ReplyPort=rp;
  216.    handle->tmh_Msg.mn_Length=sizeof(struct TMHandle);
  217.  
  218.    /* Send command to handler */
  219.    handle->tmh_Command=TMIPC_AllocTMHandle;
  220.    if (SendIPC(handle)) return(handle); /* All OK. */
  221.  
  222.    /* Something went wrong */
  223.    DeleteMsgPort(handle->tmh_Msg.mn_ReplyPort);
  224.   }
  225.   FreeMem(handle,sizeof(struct TMHandle));
  226.  }
  227.  
  228.  /* call failed */
  229.  DEBUG_PRINTF("AllocTMHandle() failed.\n");
  230.  return(NULL);
  231. }
  232.  
  233. __geta4 static void FreeTMHandle(__A0 struct TMHandle *handle)
  234. {
  235.  /* Send command to handler */
  236.  handle->tmh_Command=TMIPC_FreeTMHandle;
  237.  SendIPC(handle);
  238.  
  239.  /* Free handle */
  240.  DeleteMsgPort(handle->tmh_Msg.mn_ReplyPort);
  241.  FreeMem(handle,sizeof(struct TMHandle));
  242. }
  243.  
  244. __geta4 BOOL CreateTMObjectTagList(__A0 struct TMHandle *handle,
  245.                                    __A1 char *object,
  246.                                    __D0 ULONG type,
  247.                                    __A2 struct TagItem *tags)
  248. {
  249.  /* Sanity checks */
  250.  if ((handle==NULL) || (object==NULL) || (type>=TMOBJTYPES))
  251.   return(FALSE); /* Bad arguments! */
  252.  
  253.  /* Build IPC command */
  254.  handle->tmh_Command=TMIPC_CreateTMObject;
  255.  handle->tmh_Type=type;
  256.  handle->tmh_Object=object;
  257.  handle->tmh_Tags=tags;
  258.  
  259.  /* Send command to handler */
  260.  return(SendIPC(handle));
  261. }
  262.  
  263. /* Delete a TMObject (shared library version) */
  264. __geta4 BOOL DeleteTMObject(__A0 struct TMHandle *handle, __A1 char *object)
  265. {
  266.  /* Sanity checks */
  267.  if ((handle==NULL) || (object==NULL)) return(FALSE); /* Bad arguments! */
  268.  
  269.  /* Build IPC command */
  270.  handle->tmh_Command=TMIPC_DeleteTMObject;
  271.  handle->tmh_Object=object;
  272.  
  273.  /* Send command to handler */
  274.  return(SendIPC(handle));
  275. }
  276.  
  277. /* Change a TMObject (shared library version) */
  278. __geta4 BOOL ChangeTMObjectTagList(__A0 struct TMHandle *handle,
  279.                                    __A1 char *object,
  280.                                    __A2 struct TagItem *tags)
  281. {
  282.  /* Sanity checks */
  283.  if ((handle==NULL) || (object==NULL)) return(FALSE); /* Bad arguments! */
  284.  
  285.  /* Build IPC command */
  286.  handle->tmh_Command=TMIPC_ChangeTMObject;
  287.  handle->tmh_Object=object;
  288.  handle->tmh_Tags=tags;
  289.  
  290.  /* Send command to handler */
  291.  return(SendIPC(handle));
  292. }
  293.  
  294.